import javax.servlet.*;
import java.io.IOException;
import java.rmi.ServerException;

/**
 * Created with IntelliJ IDEA.
 * User: ttq
 * Date: 13-7-4
 * Time: 下午2:24
 * To change this template use File | Settings | File Templates.
 */
public class CharacterEncodingFilter implements Filter {
    private String characterEncoding;
    private boolean enabled;

    public void init(FilterConfig config) throws ServletException {
        characterEncoding = config.getInitParameter("characterEncoding");
        enabled = "true".equalsIgnoreCase(config.getInitParameter("enabled").trim());
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (enabled || characterEncoding != null) {
            request.setCharacterEncoding(characterEncoding);
            response.setCharacterEncoding(characterEncoding);
        }
        chain.doFilter(request, response);
    }

    public void destroy() {
    }
}
